package com.myway.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

public class HttpRequestUtil {

	
	 /**
	  * 名称：getParameterMap <br/>
	  * 描述：从request中获得参数Map，并返回可读的Map<br/>
	  * @param request
	  * @return
	  */
	@SuppressWarnings({ "unchecked", "rawtypes" })  
	public static Map getParameterMap(HttpServletRequest request) {  
	    Map properties = request.getParameterMap();  
	    Map returnMap = new HashMap();  
	    Iterator entries = properties.entrySet().iterator();  
	    Map.Entry entry;  
	    String name = "";  
	    String value = "";  
	    while (entries.hasNext()) {  
	        entry = (Map.Entry) entries.next();  
	        name = (String) entry.getKey();  
	        Object valueObj = entry.getValue();  
	        if(null == valueObj){  
	            value = "";  
	        }else if(valueObj instanceof String[]){  
	            String[] values = (String[])valueObj;  
	            for(int i=0;i<values.length;i++){  
	                value = values[i] + ",";  
	            }  
	            value = value.substring(0, value.length()-1);  
	        }else{  
	            value = valueObj.toString();  
	        }  
	        returnMap.put(name, value);  
	    }  
	    return returnMap;  
	} 
	
	
	@SuppressWarnings("unchecked")
	public static Map<String, String> getParameterMapByNames(HttpServletRequest request){
		Map<String, String> resultMap = null;
		resultMap = new HashMap<String, String>();
		Enumeration<String> requestNames = request.getParameterNames();
		while(requestNames.hasMoreElements()){
			String name = requestNames.nextElement();
			String value = request.getParameter(name);
//			value = URLDecoder.decode(value, "UTF-8");
			resultMap.put(name, value);
		}
		return resultMap;
	}
	
	public static Map<String, String> getParameterMapByStr(String str){
		Map<String, String> resultMap = null;
		try {
			if(null!=str){
				resultMap = new HashMap<String, String>();
				String[] split = str.split("&");
				for (String str2 : split) {
					System.out.println(str2);
					String[] split2 = str2.split("=");
					String key = split2[0];
					String value = null;
					if(split2.length>1){
						value = split2[1];
						value = URLDecoder.decode(value, "UTF-8");
					}
					resultMap.put(key,value);
				}
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return resultMap;
	}


	public static Map<String, String> getParameterMapByStrForALIP(String str) {
		Map<String, String> resultMap = null;
		try {
			if(null!=str){
				resultMap = new HashMap<String, String>();
				String[] split = str.split("&");
				for (String str2 : split) {
					System.out.println(str2);
					String[] split2 = str2.split("=");
					String key = split2[0];
					String value = null;
					if(split2.length>1){
						value = split2[1];
						value = new String(value.getBytes("ISO-8859-1"), "gbk");
					}
					resultMap.put(key,value);
				}
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return resultMap;
	}
	
}
